<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Booking controllers
 *
 * @author	Tran Van Thanh
 * @mail	thanhtran@vietnambiz.com
 * @date	07.09.2011
 */
require_once('admin'.EXT);

class Booking extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('booking_model'));
		$this->load->helper(array('text'));					
	}

	function index()
	{
		$page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1;

        if(isset($_GET['status'])) {
            $status = ($_GET['status'] == 'active') ? 'yes' : 'no';
            $this->db->where('status', $status);
        }

		$this->data['all_booking'] 	= $this->booking_model->get_all_booking($page != "" ? $page : 1);		
		$this->data['page'] 		= $page;

    	$conf =	array(
    		'base_url'		=> admin_url($this->router->class).'/',
    		'total_rows'	=> $this->booking_model->count_all(),
    		'per_page'		=> $this->config->item('booking', 'limit'),
    		'cur_page'		=> $page,
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav'] = $this->pagination->display_query_string();

		if($this->input->is_ajax_request()) {
			$this->load->view('booking/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'booking/index';
			$this->load->view('layout/default', $this->data);
		}
	}
	
	function view($booking_id = null)
	{		
		$booking_info = $this->booking_model->get_booking_info($booking_id);		
		$this->data['booking'] 	= $booking_info;		
		$this->load->view('booking/view', $this->data);
	}

    function delete($booking_id = null)
    {    	
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {        	
            $this->booking_model->delete_booking($booking_id);         
            die('yes');
        }
    }

	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}
			elseif($action == 'pending') {
				$this->booking_model->update_many($id_list, array('status' => 'no'));
			}			

			die('yes');
		}
	}

	function change_status()
	{
		$booking_id = $this->input->post('booking_id');
		$status = $this->input->post('status');

		$this->booking_model->update($booking_id, array('status' => $status));
		die('yes');
	}

	function load_row($booking_id = null)
	{		
		$this->data['booking'] = $this->booking_model->get($booking_id);
		$this->load->view('booking/row', $this->data);
	}	
}

